﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Profile;
using ClientApp.Models;
using CommonApp.Models;
using IBS.Bll;
using IBS.Bll.Accounts;
using IBS.Bll.Authority;
using IBS.Bll.Cards;
using IBS.Bll.KnowledgeBase;
using IBS.Bll.Payments;
using IBS.Bll.Security;
using IBS.Bll.Transfers;
using IBS.Dal;
using IBS.Model;
using IBS.ServiceContracts;
using Client = IBS.Model.Client;

namespace ClientApp.Controllers
{
    // [Authorize(Roles = "user")]
    public class TransfersController : Controller
    {
        private IAuthorityService authorityService;
        private IKnowledgeBaseService knowledgeBaseService;
        private ICardService cardService;
        private IAccountService accountService;
        private readonly OperationStep[] steps = new[]
                                            {
                                                new OperationStep { Uid="cardsMenuItem", Name = "Cards", GoTo = "/Transfers"},
                                                new OperationStep
                                                { 
                                                    Uid="receiverMenuItem", 
                                                    Name = "Destination Card",
                                                    IsVisible = false,
                                                    GoTo = "/Transfers/Receiver"
                                                },
                                                new OperationStep()
                                                    {
                                                        Uid="amountMenuItem",
                                                        Name = "Specify amount",
                                                        IsVisible = false,
                                                        GoTo = "/Transfers/SetAmount"
                                                    },
                                                     new OperationStep { Uid="homeMenuItem", Name = "Back", GoTo = "/Home/Index"},
                                            };

        public TransfersController()
        {
            var container = new IbsContainer();
            knowledgeBaseService = container.KnowledgeBaseService;
            this.authorityService = container.AuthorityService;
            this.accountService = container.AccountService;
            var transferService = container.TransferService;
            this.cardService = container.CardService;
        }

        public ActionResult Index()
        {
            IEnumerable<Card> cards = null;
            try
            {
                var client = authorityService.GetClientByUserName(User.Identity.Name);
                cards = cardService.GetClientsCard(client, c => !c.IsBlocked);
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }
            ViewBag.Steps = steps;
            ViewBag.ActiveStep = steps[0];
            ViewBag.Transfer = new TransferModel();
            ViewBag.AccountService = accountService;
            ViewBag.SelectedCardIndex = "";
            return View(cards);
        }


        public ActionResult Receiver(TransferModel transfer)
        {
            IEnumerable<Card> cards = Enumerable.Empty<Card>();

            try
            {
                Client client = authorityService.GetClientByUserName(User.Identity.Name);
                cards = cardService.GetClientsCard(client, c => !c.IsBlocked && c.Number != transfer.FromCardNumber);
            }
            catch (BusinessException be)
            {
                ModelState.AddModelError("", be.Message);
            }

            ViewBag.Steps = steps;
            steps[1].IsVisible = true;
            ViewBag.ActiveStep = steps[1];
            ViewBag.AccountService = accountService;
            ViewBag.SelectedCardIndex = "";
            ViewBag.Transfer = transfer;

            return View(cards);
        }

        [HttpPost]
        public ActionResult SetAmount(TransferModel transfer)
        {
            ViewBag.Steps = steps;
            steps[1].IsVisible = true;
            steps[2].IsVisible = true;
            ViewBag.Currencies = new[] {"USD", "BYR"};
            ViewBag.ActiveStep = steps[2];
            ViewBag.Transfer = transfer;
            ViewBag.EnsureSecurity = true;
            return View();
        }

        [HttpPost]
        public ActionResult Transfer(TransferModel transferModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var transfer = cardService.TransferToCard(transferModel.FromCardNumber,
                                transferModel.ToCardNumber, transferModel.Amount, transferModel.Currency,
                                               User.Identity.Name);
                    return View(transfer);
                }
                catch (BusinessException be)
                {
                    ModelState.AddModelError(be.Message, be.Message);
                    ViewBag.Errors = be.Message;
                }
            }
            ViewBag.Steps = steps;
            steps[1].IsVisible = true;
            steps[2].IsVisible = true;
            ViewBag.ActiveStep = steps[2];
            ViewBag.Transfer = transferModel;
            ViewBag.Currencies = new[] {"USD", "BYR"};
            return View("SetAmount");
        }
    }
}
